package com.example.administrator.caigou51.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.GoodsDetailBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.SuperDIYSwipeRefreshLayout;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQuCard2;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.List;

/**
 * Created by Administrator on 2016/8/18/018.
 */
public class SearchGoodsActivity extends GBaseActivity implements View.OnClickListener{
    MaterialListView listview;
    RelativeLayout relativeLayout_default;
    RelativeLayout relativeLayout_sales_volume;
    RelativeLayout relativeLayout_price;
    SuperDIYSwipeRefreshLayout superSwipeRefresh;
    String searchKey="";
    String order="";//排序字段sales按销量 ship_price
    String sort="desc";//排序方式 asc升序 desc降序
    List<GoodsDetailBean> goodsBeenList;
    public static final String SearchTagID="SearchTagID";

    private TextView textView_default;
    private View view_default,imageViewGoBack,linearLayoutEmpty;
    private TextView textView_sales_volume;
    private View view_sales_volume;
    private TextView textView_price;
    private ImageView imageViewTriangleUp,iv_leftimg;
    private ImageView imageViewTriangleDown,imageViewError,imageViewSearch;
    private View view_price,imageViewGoTop;
    private String catid="";
    EditText editTextSearch;
    // http://192.168.70.6/api/index.php?app=goods&act=search&pagesize=1&page=1&keyword=感冒&order=sales&sort=desc
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewFor720_1280(R.layout.activity_search_goods);
        findViewsById();
        initView();
        TaskGetSearchData(false,order,sort,searchKey,catid);
    }

    private void findViewsById() {
        listview= (MaterialListView) findViewById(R.id.listview);
        superSwipeRefresh= (SuperDIYSwipeRefreshLayout) findViewById(R.id.superSwipeRefresh);
        superSwipeRefresh.setTargetScrollWithLayout(false);
        relativeLayout_default= (RelativeLayout) findViewById(R.id.relativeLayout_default);relativeLayout_default.setOnClickListener(this);
        relativeLayout_sales_volume= (RelativeLayout) findViewById(R.id.relativeLayout_sales_volume);relativeLayout_sales_volume.setOnClickListener(this);
        relativeLayout_price= (RelativeLayout) findViewById(R.id.relativeLayout_price);relativeLayout_price.setOnClickListener(this);
        imageViewError= (ImageView) findViewById(R.id.imageViewError);imageViewError.setOnClickListener(this);
        imageViewSearch= (ImageView) findViewById(R.id.imageViewSearch);imageViewSearch.setOnClickListener(this);
        linearLayoutEmpty=findViewById(R.id.linearLayoutEmpty);
        textView_default = (TextView) findViewById(R.id.textView_default);
        view_default =  findViewById(R.id.view_default);
        imageViewGoTop=findViewById(R.id.imageViewGoTop);imageViewGoTop.setOnClickListener(this);
        imageViewGoBack=findViewById(R.id.imageViewGoBack);imageViewGoBack.setOnClickListener(this);
        textView_sales_volume = (TextView) findViewById(R.id.textView_sales_volume);
        view_sales_volume =  findViewById(R.id.view_sales_volume);
        textView_price = (TextView) findViewById(R.id.textView_price);
        imageViewTriangleUp = (ImageView) findViewById(R.id.imageViewTriangleUp);
        imageViewTriangleDown = (ImageView) findViewById(R.id.imageViewTriangleDown);
        iv_leftimg= (ImageView) findViewById(R.id.iv_leftimg);iv_leftimg.setOnClickListener(this);
        view_price =  findViewById(R.id.view_price);
        editTextSearch= (EditText) findViewById(R.id.editTextSearch);
    }
    private void initTextViewStyle() {
        textView_default.setTextColor(getResources().getColor(R.color.color_4b4f52));
        textView_price.setTextColor(getResources().getColor(R.color.color_4b4f52));
        textView_sales_volume.setTextColor(getResources().getColor(R.color.color_4b4f52));
        view_default.setVisibility(View.GONE);
        view_price.setVisibility(View.GONE);
        view_sales_volume.setVisibility(View.GONE);
    }
    private void initView() {
        getTopBar().setVisibility(View.GONE);
        App.getInstance().setScrollListViewHideBottom(listview,this);
        searchKey=getIntent().getStringExtra(SearchActivity.SearchTag);
        catid=getIntent().getStringExtra(SearchGoodsActivity.SearchTagID);
        editTextSearch.setText("");
        editTextSearch.setText(searchKey);
        if (TextUtils.isEmpty(editTextSearch.getText().toString()))
        {
            imageViewError.setVisibility(View.GONE);
        }else
        {
            imageViewError.setVisibility(View.VISIBLE);
        }
        editTextSearch.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int editStart ;
            private int editEnd ;
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                temp = s;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
//          mTextView.setText(s);//将输入的内容实时显示
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                editStart = editTextSearch.getSelectionStart();
                editEnd = editTextSearch.getSelectionEnd();

                if (temp.length() > 0) {
                    imageViewError.setVisibility(View.VISIBLE);
                }else
                {
                    imageViewError.setVisibility(View.GONE);
                }
            }
        });
        superSwipeRefresh.setOnPullRefreshListener(new SuperDIYSwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                TaskGetSearchData(false,order,sort,searchKey,catid);
            }

            @Override
            public void onPullDistance(int distance) {

            }

            @Override
            public void onPullEnable(boolean enable) {

            }
        });
        superSwipeRefresh.setOnPushLoadMoreListener(new SuperDIYSwipeRefreshLayout.OnPushLoadMoreListener() {
            @Override
            public void onLoadMore() {
                TaskGetSearchData(true,order,sort,searchKey,catid);
            }

            @Override
            public void onPushDistance(int distance) {

            }

            @Override
            public void onPushEnable(boolean enable) {

            }
        });
    }

    private void TaskGetSearchData(final boolean isLoadMore,String order,String sort,String keyword,String catid) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), listview, isLoadMore, superSwipeRefresh,
                Constant.getRootUrl() + Constant.Action.Action_SearchGoods, Constant.Action.Action_SearchGoods, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_SearchGoods,superSwipeRefresh.getPageSize()+"", superSwipeRefresh.getPage(isLoadMore)+"",keyword,catid,order,sort), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {

                        // JSONObject jsonObject = (JSONObject) result.getData();
                        goodsBeenList = JSON.parseArray(result.getData().toString(),GoodsDetailBean.class);
                        if (goodsBeenList!=null && goodsBeenList.size()>0) {
                            linearLayoutEmpty.setVisibility(View.GONE);
                            fillArrayInListView(goodsBeenList, isLoadMore);
                        }else
                        {
                            linearLayoutEmpty.setVisibility(View.VISIBLE);
                        }
                      //  Log.e("userBean",JSON.toJSONString(goodsBean));

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        linearLayoutEmpty.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId())
        {
            case R.id.imageViewGoTop:
                listview.smoothScrollToPosition(0);
                break;
            case R.id.imageViewGoBack:
                finish();
                break;
            case R.id.imageViewSearch:
                searchKey=editTextSearch.getText().toString();
                TaskGetSearchData(false,order,sort,searchKey,catid);
                break;
            case R.id.imageViewError:
                editTextSearch.setText("");
                break;
            case R.id.relativeLayout_default:
                initTextViewStyle();
                order="";
                textView_default.setTextColor(getResources().getColor(R.color.color_1ca146));
                view_default.setVisibility(View.VISIBLE);
                TaskGetSearchData(false,order,sort,searchKey,catid);
                break;
            case R.id.relativeLayout_sales_volume:
                initTextViewStyle();
                order="sales";
                textView_sales_volume.setTextColor(getResources().getColor(R.color.color_1ca146));
                view_sales_volume.setVisibility(View.VISIBLE);
                TaskGetSearchData(false,order,sort,searchKey,catid);
                break;
            case R.id.iv_leftimg:
                finish();
                break;
            case R.id.relativeLayout_price:
                initTextViewStyle();
                order="ship_price";
                if (sort.equals("desc"))
                {
                    sort="asc";
                    imageViewTriangleDown.setImageResource(R.drawable.icon_triangle_grey_down);
                    imageViewTriangleUp.setImageResource(R.drawable.icon_triangle_green_up);
                }else
                {
                    sort="desc";
                    imageViewTriangleDown.setImageResource(R.drawable.icon_triangle_green_down);
                    imageViewTriangleUp.setImageResource(R.drawable.icon_triangle_grey_up);
                }
                textView_price.setTextColor(getResources().getColor(R.color.color_1ca146));
                view_price.setVisibility(View.VISIBLE);
                TaskGetSearchData(false,order,sort,searchKey,catid);
                break;

        }
    }

    private void fillArrayInListView(List<GoodsDetailBean> list,boolean isloadmore)
    {
        if (!isloadmore)
        {

            listview.clear();
                for (int i=0;i<list.size();i++)
                {
                    ZhuangQuCard2 zhuangQuCard2=new ZhuangQuCard2(this);
                    zhuangQuCard2.setGoodsBean(list.get(i));
                    zhuangQuCard2.setShowBottomLine(true);
                    listview.add(zhuangQuCard2);
                }

        }else
        {
            for (int i=0;i<list.size();i++)
            {
                ZhuangQuCard2 zhuangQuCard2=new ZhuangQuCard2(this);
                zhuangQuCard2.setGoodsBean(list.get(i));
                listview.add(zhuangQuCard2);
            }
        }
    }


}
